package com.henriquevilela.drumtrainer.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.util.Rotation;

import com.henriquevilela.drumtrainer.core.Report;

/**
 * @author Henrique de Souza Vilela
 * henrique.vilela@gmail.com
 */
public class ReportWindow extends JDialog {

	private static final long serialVersionUID = 1L;

	public ReportWindow(JFrame owner, Report report) {
		super(owner, true);

		setPreferredSize(new Dimension(640, 480));

		JPanel root = new JPanel();
		root.setBorder(new EmptyBorder(10, 10, 10, 10));
		root.setLayout(new BorderLayout(10, 10));

		JTabbedPane tabbedPane = new JTabbedPane();

		ChartPanel pieChartPanel = createPieChart(report);
		tabbedPane.add(Messages.getString("sequence"), pieChartPanel);

		ChartPanel lineChartPanel = createLineChart(report);
		tabbedPane.add(Messages.getString("timing"), lineChartPanel);

		root.add(tabbedPane);

		setContentPane(root);

		pack();
	}

	private ChartPanel createPieChart(Report report) {
		DefaultPieDataset data = new DefaultPieDataset();
		data.setValue(Messages.getString("rigth"), report.getRigthHit());
		data.setValue(Messages.getString("wrong"), report.getWrongHit());
		data.setValue(Messages.getString("miss"), report.getMiss());

		JFreeChart chart = ChartFactory.createPieChart3D(Messages.getString("sequence"), data, true, true, false);

		PiePlot3D plot = (PiePlot3D) chart.getPlot();

		plot.setSectionPaint(Messages.getString("rigth"), new Color(0x00FF00));
		plot.setSectionPaint(Messages.getString("wrong"), new Color(0xFF0000));
		plot.setSectionPaint(Messages.getString("miss"), new Color(0xFFFF00));

		plot.setStartAngle(270);
		plot.setDirection(Rotation.ANTICLOCKWISE);
		plot.setForegroundAlpha(0.60f);
		plot.setInteriorGap(0.33);

		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(500, 270));

		return chartPanel;
	}

	private ChartPanel createLineChart(Report report) {
		XYSeries lineData = new XYSeries("teste");
		for (long[] timing : report.getTiming()) {
			lineData.add(timing[0], timing[1]);
		}

		XYSeriesCollection data = new XYSeriesCollection();
		data.addSeries(lineData);

		JFreeChart chart = ChartFactory.createXYLineChart(Messages.getString("timing"), Messages.getString("songPosition"), Messages.getString("delay"), data, PlotOrientation.VERTICAL, true, true, false);

		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(500, 270));

		return chartPanel;
	}
}